﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllenData
{
    public class InsuranceCoverageData
    {
        public InsuranceCoverage GetInsuranceCoverage(int insuranceCoverageId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                var insuranceCoverage = ctx.InsuranceCoverages.Where(d => d.InsuranceCoverageID == insuranceCoverageId);
                return insuranceCoverage.FirstOrDefault();
            }
        }

        public List<InsuranceCoverage> GetAllInsuranceCoverages()
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                return ctx.InsuranceCoverages.ToList();
            }
        }

        public int AddNewInsuranceCoverage(InsuranceCoverage insuranceCoverage)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                ctx.AddToInsuranceCoverages(insuranceCoverage);
                ctx.SaveChanges();

                InsuranceCoverage newInsuranceCoverage = ctx.InsuranceCoverages.Where(
                    d => d.Name == insuranceCoverage.Name).FirstOrDefault();

                if (newInsuranceCoverage == null)
                    return -1;
                else
                    return newInsuranceCoverage.InsuranceCoverageID;
            }
        }

        public void UpdateInsuranceCoverage(InsuranceCoverage insuranceCoverage)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                InsuranceCoverage dbDept = ctx.InsuranceCoverages.First(d => d.InsuranceCoverageID == insuranceCoverage.InsuranceCoverageID);
                ctx.ApplyCurrentValues("InsuranceCoverages", insuranceCoverage);
                ctx.SaveChanges();
            }
        }

        public void DeleteInsuranceCoverage(int insuranceCoverageId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                InsuranceCoverage dbDept = ctx.InsuranceCoverages.First(d => d.InsuranceCoverageID == insuranceCoverageId);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
